// 模块区域
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

// 路由模块区域
var loginRouter = require('./routes/login');
var registerRouter = require('./routes/register');

var app = express();

//  ejs
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
// 资源静态化
app.use(express.static(path.join(__dirname, './public')));

// 路由模块使用
app.use(loginRouter);
app.use(registerRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});


// 统一处理错误
app.use(function(err, req, res, next) {
  res.render('aa',{zhengque: err})
});

module.exports = app;
